/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.sheet.validate;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.table.TableColumn;

import twaver.ElementAttribute;
import twaver.EnumType;
import twaver.EnumTypeManager;
import twaver.Node;
import twaver.TPropertyDescriptor;
import twaver.table.TPropertySheet;
import twaver.table.TPropertySheetPane;
import demo.DemoPane;

public class ValidateValueDemo extends DemoPane {
	
    private Node node = new Node();
    private TPropertySheet sheet = new TPropertySheet();
    
    public ValidateValueDemo() {
		EnumTypeManager.getInstance().registerEnumTypes("Language", new EnumType[]{
				new EnumType(0, "C"), 
				new EnumType(1, "C++"),
				new EnumType(2, "C#"), 
				new EnumType(3, "Java"),
				new EnumType(4, "Python"),
				new EnumType(5, "Pascal"),
				new EnumType(6, "Fortran"),
				new EnumType(7, "PHP"),
		});	
		
        EnumTypeManager.getInstance().registerEnumTypes("City", new EnumType[] {
                new EnumType("N", "New York"),
                new EnumType("T", "Tokyo"),
                new EnumType("L", "London")});
		
		EnumTypeManager.getInstance().registerEnumTypes("State", new EnumType[]{
				new EnumType('s', "STOPPED", "/demo/sheet/validate/stopped.gif"), 
				new EnumType('r', "RUNNING", "/demo/sheet/validate/running.gif")});	
    	
    	node.putClientProperty("MyProp1", "ABC");
    	node.putClientProperty("MyProp2", "123456789@#$%^&");       
    	node.putClientProperty("Married", Boolean.TRUE);
    	node.putClientProperty("Scale", new Float(0.1));
    	node.putClientProperty("Color", new Color(10, 90, 51));
    	node.putClientProperty("State", 's');      
    	node.putClientProperty("City", "N");
    	node.putClientProperty("Language", 53); 
    	node.putClientProperty("Character", new Character('T'));
    	node.putClientProperty("Proportion", new Double(0.8));       
    	
    	sheet.setEditable(true);
        sheet.registerElementClassXML(Node.class, "/demo/sheet/validate/validate.xml");
        sheet.setElement(node);
        
        TableColumn column = sheet.getColumnModel().getColumn(0);
        sheet.getTableHeader().setResizingColumn(column);
        column.setWidth(200);       
        
    	JButton button = new JButton("Validate Value");
        this.add(new TPropertySheetPane(sheet, false), BorderLayout.CENTER);
        this.add(button, BorderLayout.SOUTH);
        button.setBackground(Color.YELLOW);
        button.setForeground(Color.BLACK);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	validateValue();
            }
        });

    }
    
    public void validateValue(){
    	if(sheet.getCellEditor() != null){
    		sheet.getCellEditor().stopCellEditing();
    	}
    	
    	List properties = sheet.getPropertyDescriptorsByCategoryName("group1");//sheet.getAllPropertyDescriptors();
    	for(int i=0; i<properties.size(); i++){
    		TPropertyDescriptor property = (TPropertyDescriptor)properties.get(i);
    		ElementAttribute attribute = property.getElementAttribute();
    		Object value = sheet.getElementPropertyValue(property);
    		
    		// check minLength
    		if(attribute.getParam("minLength") != null){
    			int minLength = Integer.parseInt(attribute.getParam("minLength").toString());
    			if(value == null || value.toString().length() < minLength){
    				showMessage(attribute);
    				return;
    			}
    		}
    		
    		// check maxLength
    		if(attribute.getParam("maxLength") != null){
    			int maxLength = Integer.parseInt(attribute.getParam("maxLength").toString());
    			if(value != null && value.toString().length() > maxLength){
    				showMessage(attribute);
    				return;
    			}
    		}
    		
    		// check nullable
    		if("false".equals(attribute.getParam("nullable")) && value == null){
				showMessage(attribute);
				return;
    		}
    		
    		// check minValue
    		if(attribute.getParam("minValue") != null){
    			double minValue = Double.parseDouble(attribute.getParam("minValue").toString());
    			if(value != null && Double.parseDouble(value.toString()) < minValue){
    				showMessage(attribute);
    				return;
    			}
    		}
    		
    		// check maxValue
    		if(attribute.getParam("maxValue") != null){
    			double maxValue = Double.parseDouble(attribute.getParam("maxValue").toString());
    			if(value != null && Double.parseDouble(value.toString()) > maxValue){
    				showMessage(attribute);
    				return;
    			}
    		}
    		
    	}
    	
		JOptionPane.showMessageDialog(this, "Congratulate! All values is valid.");
    }

    private void showMessage(ElementAttribute attribute){
    	JOptionPane.showMessageDialog(this, attribute.getParam("message"));
    	sheet.selectRow(attribute);
    }

}